import cv2
from socket import *
import time


cap = cv2.VideoCapture(1)
# 统一图像大小为640*480
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)

Host = 'localhost'
Port = 5555
sock = socket(AF_INET, SOCK_DGRAM)  # 创建UDP套接字
print("Sending...")

while True:
    try:
        time.sleep(0.1)
        ret, img = cap.read()
        # cv2.imshow('video', img)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        ret, send_data = cv2.imencode('.jpg', img, [cv2.IMWRITE_JPEG_QUALITY, 50])  # 将图片转为byte
        sock.sendto(send_data, (Host, Port))

    except:
        pass

cap.release()
cv2.destroyAllWindows()

